#!/bin/sh -e

function createIphoneos {
    echo "▸Create ${filename}.framework for iphoneos"
    cp -R ./input/${1}.framework  iphoneos
    lipo -remove i386 -remove x86_64 iphoneos/${1}.framework/${1} -o iphoneos/${1}.framework/${1}
    lipo -info iphoneos/${1}.framework/${1}
}

function createIphoneSimulator {
    echo "▸Create ${filename}.framework for iphonesimulator"
    cp -R ./input/${1}.framework  iphonesimulator
    lipo -remove armv7 -remove arm64 -remove armv7s iphonesimulator/${1}.framework/${1} -o iphonesimulator/${1}.framework/${1}
    lipo -info iphonesimulator/${1}.framework/${1}
}

function combineFramework {
    echo "▸ Making ${1}.xcframework"
    xcodebuild -create-xcframework -framework iphoneos/${1}.framework -framework iphonesimulator/${1}.framework -output "./output/${1}.xcframework"
    echo "▸ Make ${1}.xcframework Success"
}

echo "▸ Create Temp Folder"
mkdir iphoneos iphonesimulator

for file in ./input/*
do
    filename=$(basename ${file} .framework)
    echo "############"
    echo "▸Begin Make ${filename}.xcframework"
    createIphoneos $filename
    createIphoneSimulator $filename
    combineFramework $filename
done


echo "▸ Clean Temp Folder"
rm -rf iphoneos iphonesimulator

